<!--文章分类页 ArticleView.vue-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;font-weight: bold">信息管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="beforeAddBusiness">新增分类</el-button>
  </div>
  <!-- 分类查询条件卡片 -->
  <el-card style="margin:20px;height: 80px;">
    <el-form :inline="true">
      <el-form-item label="分类名称">
        <el-input placeholder="请输入" style="width:300px;" v-model="searchForm.name" ></el-input>
      </el-form-item>
      <el-form-item label="门店状态" style="width:300px;">
        <el-select placeholder="请选择" v-model="searchForm.status" @change="load">
          <el-option label="全部" value="0"/>
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="load">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 店铺列表表格数据 -->
  <el-card style="margin:20px;">
    <h3>分类列表</h3>
    <el-table stripe :data="tableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="名称" prop="categoryName" align="center"></el-table-column>
      <el-table-column label="排序" prop="sort" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" ></el-table-column>
      <el-table-column label="更新时间" prop="updateTime" align="center"></el-table-column>
      <el-table-column label="启用状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"
                     @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="edit">编辑</el-button>
          <el-button type="primary" link @click="deleteStore(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 新建门店弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width: 1000px;padding: 40px;" :before-close="handleClose">
    <el-form>
      <el-form-item label="分类名称">
        <el-input placeholder="请输入分类名称" v-model="newForm.categoryName" ></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input placeholder="请输入排序" v-model="newForm.sort" ></el-input>
      </el-form-item>
      <el-form-item label="门店状态">
        <el-radio-group  v-model="newForm.categoryStatus">
          <el-radio  label="开启" value="1" ></el-radio>
          <el-radio  label="禁用" value="0"></el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button  type="primary" @click="save">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
const dialogTitle=ref("新增用户");
const dialogVisible = ref(false);
const beforeAddBusiness=()=>{
  dialogVisible.value = true;
}
const searchForm = ref({
  categoryName: '',
  categoryStatus: ''});
const tableData = ref([]);
const load = ()=>{
  // let data = qs.stringify(searchForm.value);
  axios.post(BASE_URL+'2020/admin/content/category/list',searchForm.value).then((response)=>{
    console.log("前端"+searchForm.value);
    if(response.data.code == 0){
      tableData.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  load();
})
const resetSearch = ()=>{
  searchForm.value = {name: '', status: ''};
  load();
}
const newForm = ref({
  name: '',
  sort: '',
  status: '',
});
const changeStatus=(id,status)=>{

}
const save = ()=>{
  axios.post(BASE_URL+'2020/admin/content/category/add',newForm.value).then((response)=>{
    if(response.data.code == '0'){
      ElMessage.success('保存成功');
      dialogVisible.value = false;
      dialogTitle.value="新增用户";
      load();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//定义删除用户的方法
const deleteStore=(id)=>{
  axios.post(BASE_URL+'2020/admin/content/category/delete'+id)
      .then((response)=>{
        if(response.status===200){
          ElMessage.success('您确认要删除吗！');
          load();
        }else{
          ElMessage.error('删除失败');
        }
      })
}
// 定义处理弹窗关闭的方法
const handleClose=()=>{
  if (confirm('确定要取消保存吗？')) {
    newForm.value = {};
    dialogVisible.value = false;
  }
}
//定义编辑的方法
const edit=()=>{
  // axios.post(BASE_URL+'2020/admin/content/article/update',newForm.name).then((response)=>{
  //   if(response.data.code == '0'){
  //     ElMessage.success('保存成功');
  //     dialogVisible.value = false;
  //     dialogTitle.value="编辑用户";
  //     load();
  //   }else{
  //     ElMessage.error(response.data.msg);
  //   }
  // })
}
</script>



<style scoped>
</style>